import dayjs from 'dayjs'
import Schema from 'validate'

const fromInfo = new Schema({
  houseId:{
    type:String,
    required:true,
    message:'亲添加房屋'
  },
  repairItemId:{
    type:String,
    required:true,
    message:'亲添加问题类型'
  },
  mobile:{
    type:String,
    required:true,
    message:'请添加手机号'
  },
  appointment:{
    type:String,
    required:true,
    message:'请添加时间'
  },
  description:{
    type:String,
    required:true,
    length:{min:1,max:200},
    message:'请描述问题，限200字'
  },
  attachment:{
    type:Array,
    length:{min:0,max:9},
    each:{
      id:{type:String,required:true,},
      url:{type:String,required:true,}
    },
    message:'请选择图片'
  },
})
Page({
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    maxDate:+dayjs().add(3,'day'),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [
      { name: '北京西三旗花园1号楼 101' },
      { name: '北京东村家园3号楼 302' },
      { name: '北京育新花园3号楼 703' },
      { name: '北京天通苑北苑8号楼 403' },
    ],
    houseName:'',
    houseId:'',
    repairItem:[],
    repairItemId:'',
    repairName:'',
    // 时间
    appointment:'',
     // 照片
    attachment: [],
    mobile:'',
    description:'',
    id:''
  },
  onLoad({id}){
    if(id){
      this.getIdDetail(id)
      wx.setNavigationBarTitle({
        title: '修改保修信息',
      })
    }
    this.getHouseList()
    this.getRepairList()
  },
  // 查询待id的维修信息
  async getIdDetail(id){
    const res = await wx.http({url:`/repair/{id}`})
    console.log(res);
  },
  // 删除图片
  delPic(e){
    wx.showModal({
      title: '删除',
      content: `你确定要删除这张照片吗?`,
      success: ({confirm}) => {
        if (confirm) {
          const { attachment } = this.data
          attachment.splice(e.detail.index,1)
          this.setData({attachment})
        }
      }
    })
  },
  // 上传图片
  afterRead(e){
     wx.uploadFile({
       filePath: e.detail.file.url,
       name: 'file',
       url: wx.http.baseURL + '/upload',
       header:{
         Authorization:`Bearer ${getApp().token}`
       },
       success: res => {
         console.log('555',res);
         const {data} = JSON.parse(res.data)
         this.setData({attachment:[...this.data.attachment,data]})
       }
     })
  },
  // 选中日期
  selectDate(e){
    this.setData({
      currentDate:e.detail,
      appointment:dayjs(e.detail).format('YYYY-MM-DD'),
      dateLayerVisible:false
    })
  },
  // 获取维修项目
  async getRepairList(){
    const res = await wx.http({url:'/repairItem'})
    this.setData({repairItem:res.data})
  },
  // 选中维修
  onRepairItemSelect(e){
    this.setData({repairItemId:e.detail.id,repairName:e.detail.name})
  },
  // 房屋选中事件
  onHouseSelect(e){
    const {id,name} = e.detail
    this.setData({houseId:id,houseName:name})
  },
  // 获取下拉房子
  async getHouseList(){
    const res = await wx.http({url:'/house'})
    this.setData({houseList:res.data})
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 提交审核
  async goList() {
    const {houseId,repairItemId,mobile,appointment,description, attachment} = this.data
    const data = {houseId,repairItemId,mobile,appointment,description, attachment}
    const errors = fromInfo.validate(data)
    if(errors.length){
      return wx.utils.toast(errors[0].message)
    }
    await wx.http({
      url:'/repair',
      method:'post',
      data
    })
    wx.showToast({
      title: '成功',
      icon:'success'
    })
    setTimeout(()=>{
       wx.navigateTo({
         url: '/repair_pkg/pages/list/index',
       })
    },2000)
    // wx.reLaunch({
    //   url: '/repair_pkg/pages/list/index',
    // })
  },
})
